<template>
    <div class="flex_col card m5 p10">
        <label class="flex_center_self">扫码测试</label>
        <div class="flex_row_center mt5">
            <el-button type="primary" @click="onClickScan">当前位置 扫码</el-button>
            <el-button @click="onClickLedOpen">补光灯打开</el-button>
            <el-button @click="onClickLedClose">补光灯关闭</el-button>
        </div>
        
        <div class="flex_row_center mt5">
            <label>行:</label>
            <el-select v-model="ctx.select_row" style="width: 100px;">
                <el-option v-for="(item, index) in ROWS" :key="index" :label="item" :value="item" />
            </el-select>
            <el-button type="success" class="ml5" @click="onClickScanRow">扫码一整行</el-button>
            <el-button class="mt5" type="warning" @click="onClickScanAll">扫码整个货架</el-button>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { cargoCtrl, cargoScan, comprReset, pickCtrl, scanLedCtrl } from '@/service/devices/cargo';
import { cargo_position_with_col, cargo_scan_position_with_row, ROWS } from '@/utils/config';
import { delay, runAction, runAction3 } from '@/utils/ui';
import { reactive } from 'vue';

interface Ctx {
    select_row: number,
}

interface Props {
    index: number,
}

const props = defineProps<Props>();
const ctx = reactive<Ctx>({
    select_row: 1
});

async function onClickScan() {
    await runAction3('扫码', '', async () => {
        await scanLedCtrl({
            index: props.index,
            ctrl: 0
        });
        const msg = await cargoScan(props.index);
        await scanLedCtrl({
            index: props.index,
            ctrl: 1
        });
        return msg;
    })
}

async function onClickScanRow() {
    await runAction('扫码一整行', '', async (dialog) => {
        const row = ctx.select_row;
        const position = cargo_scan_position_with_row(row);
        await comprReset(props.index);
        await cargoCtrl({
            index: props.index,
            position
        });
        await scanLedCtrl({
            index: props.index,
            ctrl: 0
        });

        for (let col = 1; col <= 6; col++) {
            await pickCtrl({
                index: props.index,
                position: cargo_position_with_col(col),
            });
            try {
                const barcode = await cargoScan(props.index);
                dialog.setMsg(`${row}-${col}: ${barcode}`);
            } catch (e) {
                dialog.setMsg(`${row}-${col} ${e}`)
            }
        }
        await scanLedCtrl({
            index: props.index,
            ctrl: 1
        });

        await delay(2000);
    });
}

async function onClickScanAll() {
    await runAction('扫码全部', '', async (dialog) => {
        await comprReset(props.index);
        await scanLedCtrl({
            index: props.index,
            ctrl: 0
        });
        for (let row = 1; row <= 25; row++) {

            let position = cargo_scan_position_with_row(row);
            await cargoCtrl({
                index: props.index,
                position
            });

            for (let col = 1; col <= 6; col++) {
                await pickCtrl({
                    index: props.index,
                    position: cargo_position_with_col(col),
                });
                try {
                    const barcode = await cargoScan(props.index);
                    dialog.setMsg(`${row}-${col}: ${barcode}`);
                } catch (e) {
                    dialog.setMsg(`${row}-${col} ${e}`)
                }
            }

            row += 1;
            if (row > 25) {
                break;
            }

            position = cargo_scan_position_with_row(row);
            await cargoCtrl({
                index: props.index,
                position
            });

            for (let col = 6; col >= 1; col--) {
                await pickCtrl({
                    index: props.index,
                    position: cargo_position_with_col(col),
                });
                try {
                    const barcode = await cargoScan(props.index);
                    dialog.setMsg(`${row}-${col}: ${barcode}`);
                } catch (e) {
                    dialog.setMsg(`${row}-${col} ${e}`)
                }
            }
        }
        await scanLedCtrl({
            index: props.index,
            ctrl: 1
        });
    });
}

async function onClickLedOpen() {
    runAction('补光灯控制', '打开', async () => {
        await scanLedCtrl({
            index: props.index,
            ctrl: 0
        });
    })
}

async function onClickLedClose() {
    runAction('补光灯控制', '关闭', async () => {
        await scanLedCtrl({
            index: props.index,
            ctrl: 1
        });
    })
}

</script>
